﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Gs;
using Microsoft.Win32;
using System.Configuration;
using System.Reflection;
using DemoReception.OpenWindow;
namespace DemoReception
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private GsToast iToast;
        private string iHotelCode;
        private ContextMenu iRightClickMenu;
        private List<CHotelRoomCatalog> RoomList;
        public MainWindow(string aCode)
        {
            iHotelCode = aCode;
            InitializeComponent();
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            Version version = executingAssembly.GetName().Version;
            this.Title = "模拟房态工具 v" + version.ToString();
            iToast = new GsToast();
            iToast.BindControl(this.Content as Grid);
            rRoomControl.MouseRightButtonUp += new MouseButtonEventHandler(rRoomControl_MouseRightButtonUp);
            rRoomControl.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(rRoomControl_MouseLeftButtonDown);

            this.Loaded += new RoutedEventHandler(RoomStatView_Loaded);
            CHotelDelegate.GetHotelEvent().iRefreshEvent += new CHotelEvent.RefreshRoomEvent(RefreshRoom);
        }

        void RoomStatView_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshRoom(iHotelCode);
        }

        public void RefreshRoom(string aHotelCode)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                int Rst = CHotelController.GetRoomStatList(out RoomList, aHotelCode);
                if (Rst < 0)
                {
                    iToast.Show("获取客房信息失败");
                }
                if (RoomList != null)
                {
                    InitRoomStat();
                    UpdateRoomStatNum();
                }
            }));
        }

        public void InitRoomStat()
        {
            rRoomControl.iDefaultItemsSource = null;
            rRoomControl.iDefaultItemsSource = RoomList;
        }

        void UpdateRoomStatNum()
        {
            int VC = 0, VD = 0, OC = 0, OD = 0, OS = 0, ED = 0, EA = 0, Total = 0, Settle = 0;
            foreach (CHotelRoomCatalog Rooms in RoomList)
            {
                foreach (CRoomStat Room in Rooms.iRooms)
                {
                    Total++;
                    if (Room.iRoomStatCode == CRoomStatusCode.KEmptyClean)
                    {
                        VC++;
                    }
                    else if (Room.iRoomStatCode == CRoomStatusCode.KEmptyDirty)
                    {
                        VD++;
                    }
                    else if (Room.iRoomStatCode == CRoomStatusCode.KSettleClean)
                    {
                        Settle++;
                        OC++;
                    }
                    else if (Room.iRoomStatCode == CRoomStatusCode.KSettleDirty)
                    {
                        Settle++;
                        OD++;
                    }
                    else
                    {
                        OS++;
                    }
                    if (Room.iGuestStatCode == CGuestStatusCode.KToLeave)
                    {
                        ED++;
                    }
                    else if (Room.iGuestStatCode == CGuestStatusCode.KToArrive)
                    {
                        EA++;
                    }
                }
            }
            rEmptyCleanButton.iValue = VC;
            rEmptyDirtyButton.iValue = VD;
            rSettleCleanButton.iValue = OC;
            rSettleDirtyButton.iValue = OD;
            rExceptButton.iValue = OS;
            rToLeaveButton.iValue = ED;
            rToArriveButton.iValue = EA;
            rRoomSettleNumText.Text = Settle + "/" + Total;
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            RefreshRoom(iHotelCode);
            iToast.Show("房态已刷新");
        }

        private void NewRoom_Click(object sender, RoutedEventArgs e)
        {
            RoomSetting RoomWindow = new RoomSetting(iHotelCode, 1);
            RoomWindow.Show();
        }

        private void NewFloor_Click(object sender, RoutedEventArgs e)
        {
            RoomSetting RoomWindow = new RoomSetting(iHotelCode, 0);
            RoomWindow.Show();
        }

        private void NewBooking_Click(object sender, RoutedEventArgs e)
        {
            GuestView GuestWindow = new GuestView(iHotelCode);
            GuestWindow.Show();
        }

        void rRoomControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            rRoomControl.ContextMenu = null;
            if (iRightClickMenu == null) iRightClickMenu = new ContextMenu();
            iRightClickMenu.Items.Clear();
            MenuItem item;
            CRoomItem RoomItem = CVisualTreeHelper.GetParent<CRoomItem>(e.OriginalSource as DependencyObject);
            if (RoomItem != null)
            {
                CRoomStat Room = RoomItem.DataContext as CRoomStat;
                if (Room == null || string.IsNullOrEmpty(Room.iLocation)) return;
               
                switch (Room.iRoomStatCode)
                {
                    case CRoomStatusCode.KEmptyClean:
                    case CRoomStatusCode.KSettleClean:
                        item = new MenuItem();
                        item.Header = "转肮脏";
                        item.Name = CRoomOperation.KDirty;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        break;
                    case CRoomStatusCode.KEmptyDirty:
                    case CRoomStatusCode.KSettleDirty:
                        item = new MenuItem();
                        item.Header = "转干净";
                        item.Name = CRoomOperation.KClean;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        break;
                    case CRoomStatusCode.KExcept:
                        item = new MenuItem();
                        item.Header = "净锁";
                        item.Name = CRoomOperation.KCleanLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        item = new MenuItem();
                        item.Header = "脏锁";
                        item.Name = CRoomOperation.KDirtyLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        break;
                    case CRoomStatusCode.KLockClean:
                        item = new MenuItem();
                        item.Header = "未知锁";
                        item.Name = CRoomOperation.KUnKnowLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        item = new MenuItem();
                        item.Header = "脏锁";
                        item.Name = CRoomOperation.KDirtyLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        break;
                    case CRoomStatusCode.KLockDirty:
                        item = new MenuItem();
                        item.Header = "未知锁";
                        item.Name = CRoomOperation.KUnKnowLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        item = new MenuItem();
                        item.Header = "净锁";
                        item.Name = CRoomOperation.KCleanLock;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                        break;
                }
                if(Room.iRoomStatCode == CRoomStatusCode.KSettleClean|| Room.iRoomStatCode == CRoomStatusCode.KSettleDirty)
                {
                    item = new MenuItem();
                    item.Header = "退房";
                    item.Name = CRoomOperation.KCheckOut;
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                    item = new MenuItem();
                    item.Header = "延长入住";
                    item.Name = CRoomOperation.KOverStay;
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                }
                else if (Room.iRoomStatCode == CRoomStatusCode.KEmptyDirty || Room.iRoomStatCode == CRoomStatusCode.KEmptyClean)
                {
                    if (Room.iGuestStatCode == CGuestStatusCode.KToArrive)
                    {
                        item = new MenuItem();
                        item.Header = "入住";
                        item.Name = CRoomOperation.KCheckIn;
                        item.Tag = Room.iLocId;
                        item.Click += new RoutedEventHandler(RoomItem_RightClick);
                        iRightClickMenu.Items.Add(item);
                    }
                    item = new MenuItem();
                    item.Header = "未知锁";
                    item.Name = CRoomOperation.KUnKnowLock;
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                    item = new MenuItem();
                    item.Header = "净锁";
                    item.Name = CRoomOperation.KCleanLock;
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                    item = new MenuItem();
                    item.Header = "脏锁";
                    item.Name = CRoomOperation.KDirtyLock;
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                }
                else
                {
                    item = new MenuItem();
                    item.Header = "解锁";
                    if (Room.iRoomStatCode == CRoomStatusCode.KExcept)
                    {
                        item.Name = CRoomOperation.KUnLockDirty;
                    }
                    else
                    {
                        item.Name = CRoomOperation.KUnLock;
                    }
                    item.Tag = Room.iLocId;
                    item.Click += new RoutedEventHandler(RoomItem_RightClick);
                    iRightClickMenu.Items.Add(item);
                }
                item = new MenuItem();
                item.Header = "取消预约";
                item.Name = CRoomOperation.KCancelBooking;
                item.Tag = Room.iLocId;
                item.Click += new RoutedEventHandler(RoomItem_RightClick);
                iRightClickMenu.Items.Add(item);
            }
            else
            {
                Border RoomTypeItem = CVisualTreeHelper.GetParent<Border>(e.OriginalSource as DependencyObject);
                if (RoomTypeItem == null|| RoomTypeItem.Tag==null) return;
                item = new MenuItem();
                item.Header = "修改描述";
                item.Name = "edit";
                item.Tag =new KeyValuePair<Int64,string>((Int64)RoomTypeItem.Tag,RoomTypeItem.ToolTip.ToString());
                item.Click += new RoutedEventHandler(RoomTypeItem_RightClick);
                iRightClickMenu.Items.Add(item);
                item = new MenuItem();
                item.Header = "删除房型";
                item.Name = "del";
                item.Tag = new KeyValuePair<Int64, string>((Int64)RoomTypeItem.Tag, RoomTypeItem.ToolTip.ToString());
                item.Click += new RoutedEventHandler(RoomTypeItem_RightClick);
                iRightClickMenu.Items.Add(item);
            }
            rRoomControl.ContextMenu = iRightClickMenu;
        }

        void rRoomControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            /*
            CRoomItem clickRoom = CVisualTreeHelper.GetParent<CRoomItem>(e.OriginalSource as DependencyObject);
            if (clickRoom == null) return;
            CRoomStatNode contextNode = clickRoom.DataContext as CRoomStatNode;
            if (contextNode.iRoomStatSource == null) return;

            RoomDetailWindow win = new RoomDetailWindow(contextNode.iRoomStatSource);
            win.Owner = CAppClientModel.GetMainWindow();
            win.ShowDialog();
            ToRefreshRoomStat();*/
        }

        void RoomTypeItem_RightClick(object sender, RoutedEventArgs e)
        {
            MenuItem ClickItem = (MenuItem)sender;
            if(ClickItem.Name=="edit")
            {
                KeyValuePair<Int64, string> RoomType = (KeyValuePair <Int64, string>)ClickItem.Tag;
                RoomSetting SettingWindow = new RoomSetting(iHotelCode, 0);
                SettingWindow.SetRoomType(RoomType.Key, RoomType.Value);
                SettingWindow.Show();
            }
            else if(ClickItem.Name == "del")
            {
                KeyValuePair<Int64, string> RoomType = (KeyValuePair<Int64, string>)ClickItem.Tag;
                int Rst = CHotelController.SetHotelRoomType(iHotelCode, RoomType.Key*(-1), RoomType.Value);
                if (Rst < 0)
                {
                    iToast.Show("删除分类失败!");
                }
                RefreshRoom(iHotelCode);
            }
        }

        void RoomItem_RightClick(object sender, RoutedEventArgs e)
        {
            MenuItem ClickItem = (MenuItem)sender;
            Int64 RoomId = Convert.ToInt64(ClickItem.Tag);
            int Rst = Lmm.LmmErrCode.KErrParam;
            string Operation= ClickItem.Name;
            switch (Operation)
            {
                case CRoomOperation.KClean:
                case CRoomOperation.KDirty:
                case CRoomOperation.KUnKnowLock:
                case CRoomOperation.KCleanLock:
                case CRoomOperation.KDirtyLock:
                case CRoomOperation.KUnLock:
                case CRoomOperation.KUnLockDirty:
                    Rst = CHotelController.UpdateRoomStat(iHotelCode, RoomId, Operation);break;
                case CRoomOperation.KCheckIn:
                case CRoomOperation.KCheckOut:
                    Rst = CHotelController.CheckInAndOut(iHotelCode, RoomId, Operation);break;
                case CRoomOperation.KCancelBooking:
                    {
                        BookingView bv = new BookingView(iHotelCode, RoomId);
                        bv.Show();
                        break;
                    }
                case CRoomOperation.KOverStay:
                    {
                        TimeView tv = new TimeView(iHotelCode, RoomId);
                        tv.Show();
                        break;
                    }
                
            }
            if(Rst<0)
            {
                iToast.Show("房态更新出错!"); return;
            }
            RefreshRoom(iHotelCode);
            return;
        }

        private void DailyUpd_Click(object sender, RoutedEventArgs e)
        {
            int Rst=CHotelController.SetDailyDirty(iHotelCode);
            if (Rst < 0)
            {
                iToast.Show("房态更新出错!"); return;
            }
            RefreshRoom(iHotelCode);
        }
    }
}
